/**
 * 
 */
package moviles.turnomovil.interfaz;

import moviles.app.mundo.R;
import moviles.turnomovil.mundo.Alerta;
import moviles.turnomovil.mundo.TurnoMovil;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * @author juanjo
 *
 */
public class SelectActivity extends Activity {

	public static final int EDITAR_ALERTA_SELECT = 1;
	
	public static final int ELIMINAR_ALERTA_SELECT = 2;
	
	public static final String TIPO_SELECT = "tipo";
	
	
	private TextView lbSelect;
	
	private Spinner spSelect;
	
	private ArrayAdapter<Alerta> adapterAlertas;
	
	private int tipoSelect;
	
	private Alerta alertaSeleccionada;
	
	private Button btnContinuar;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lyselect);
		
		tipoSelect = getIntent().getIntExtra(TIPO_SELECT, 1);
		lbSelect = (TextView) findViewById(R.id.lbSelect);
		
		if( tipoSelect == EDITAR_ALERTA_SELECT )
			initEditarAlerta();
		else if( tipoSelect == ELIMINAR_ALERTA_SELECT )
			initEliminarAlerta();
	}

	private void initEliminarAlerta() {
		lbSelect.setText(R.string.lbSelectAlerta);
		lbSelect.refreshDrawableState();
		
		btnContinuar = (Button)findViewById(R.id.btnContinuar);
		btnContinuar.setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View v) {
				mostrarDialogEliminar();
			}
		});
		
		spSelect = (Spinner) findViewById(R.id.spAlertas);
		
		adapterAlertas = new ArrayAdapter<Alerta>(this, android.R.layout.simple_spinner_item);
		
		Alerta[] alertas = TurnoMovil.getInstance().getAlertas();
		
		for( int i= 0; i < alertas.length ; i++ )
		{
			if(alertas[i] != null )
				adapterAlertas.add(alertas[i]);
		}
		
		spSelect.setAdapter(adapterAlertas);
		
	}

	private void initEditarAlerta() {
		lbSelect.setText(R.string.lbSelectAlerta);
		lbSelect.refreshDrawableState();
		
		btnContinuar = (Button)findViewById(R.id.btnContinuar);
		btnContinuar.setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View v) {
				mostrarEditarAlerta();
			}
		});
		
		spSelect = (Spinner) findViewById(R.id.spAlertas);
		
		adapterAlertas = new ArrayAdapter<Alerta>(this, android.R.layout.simple_spinner_item);
		
		Alerta[] alertas = TurnoMovil.getInstance().getAlertas();
		

		for( int i= 0; i < alertas.length ; i++ )
		{
			if(alertas[i] != null )
				adapterAlertas.add(alertas[i]);
		}
		
		spSelect.setAdapter(adapterAlertas);
		
	}
	
	protected void mostrarDialogEliminar() {
		showDialog(0);
	}

	protected Dialog onCreateDialog(int id) {
	    
	    return dialogoEliminar();
	}
	
	/**
	 * Crea un dialogo de error indicando los errores de interacci�n
	 * @return un dialogo indicando errores de interacci�n
	 */
	private Dialog dialogoEliminar(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Va a borrar la alerta, ¿está seguro?");
		builder.setCancelable(true);
		builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		               eliminarAlerta(); 
		        	   dialog.cancel();
		           }
		       });
		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;
	}
	
	private void eliminarAlerta() {
		alertaSeleccionada = (Alerta)spSelect.getSelectedItem();
		TurnoMovil.getInstance().removeAlerta(alertaSeleccionada.getId());
		finish();
	}
	
	public void mostrarEditarAlerta() {
		alertaSeleccionada = (Alerta)spSelect.getSelectedItem();
		Intent mensaje = new Intent(this, EditAlertasActivity.class);
		mensaje.putExtra(EditAlertasActivity.TIPO, EditAlertasActivity.EDITAR_ALERTA);
		mensaje.putExtra(EditAlertasActivity.ID_ALERTA, alertaSeleccionada.getId());
		startActivity(mensaje);
	}
}
